From 288abd9c40461a00d6a52b40153fbf13c423bc1f Mon Sep 17 00:00:00 2001 From: Paul Donald Date: Sun, 19 Oct 2025 19:30:38 +0200 Subject: [PATCH] dhcpv6: move dhcpv6 message type check for early exit MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Avoid doing a bunch of work if the message type we received is not a client type and does not warrant a reply. Exit early. Clarify and comment message type handling. Signed-off-by: Paul Donald Link: https://github.com/openwrt/odhcpd/pull/279 Signed-off-by: Álvaro Fernández Rojas --- src/dhcpv6.c | 56 ++++++++++++++++++++++++++++------------------------ src/dhcpv6.h | 2 ++ 2 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/dhcpv6.c b/src/dhcpv6.c index 73ce0b3..b92790e 100644 --- a/src/dhcpv6.c +++ b/src/dhcpv6.c @@ -318,6 +318,36 @@ static void handle_client_request(void *addr, void *data, size_t len, if (len < sizeof(*hdr)) return; + switch (hdr->msg_type) { + /* Valid message types for clients */ + case DHCPV6_MSG_SOLICIT: + case DHCPV6_MSG_REQUEST: + case DHCPV6_MSG_CONFIRM: + case DHCPV6_MSG_RENEW: + case DHCPV6_MSG_REBIND: + case DHCPV6_MSG_RELEASE: + case DHCPV6_MSG_DECLINE: + case DHCPV6_MSG_INFORMATION_REQUEST: + case DHCPV6_MSG_RELAY_FORW: +#ifdef DHCPV4_SUPPORT + /* if we include DHCPV4 support, handle this message type */ + case DHCPV6_MSG_DHCPV4_QUERY: +#endif + break; + /* Invalid message types for clients i.e. server messages */ + case DHCPV6_MSG_ADVERTISE: + case DHCPV6_MSG_REPLY: + case DHCPV6_MSG_RECONFIGURE: + case DHCPV6_MSG_RELAY_REPL: +#ifndef DHCPV4_SUPPORT + /* if we omit DHCPV4 support, ignore this client message type */ + case DHCPV6_MSG_DHCPV4_QUERY: +#endif + case DHCPV6_MSG_DHCPV4_RESPONSE: + default: + return; + } + debug("Got a DHCPv6-request on %s", iface->name); /* Construct reply message */ @@ -566,32 +596,6 @@ static void handle_client_request(void *addr, void *data, size_t len, if (hdr->msg_type == DHCPV6_MSG_RELAY_FORW) handle_nested_message(data, len, &hdr, &opts, &opts_end, iov); - switch (hdr->msg_type) { - case DHCPV6_MSG_SOLICIT: - case DHCPV6_MSG_REQUEST: - case DHCPV6_MSG_CONFIRM: - case DHCPV6_MSG_RENEW: - case DHCPV6_MSG_REBIND: - case DHCPV6_MSG_RELEASE: - case DHCPV6_MSG_DECLINE: - case DHCPV6_MSG_INFORMATION_REQUEST: - case DHCPV6_MSG_RELAY_FORW: -#ifdef DHCPV4_SUPPORT - case DHCPV6_MSG_DHCPV4_QUERY: -#endif - break; /* Valid message types for clients */ - case DHCPV6_MSG_ADVERTISE: - case DHCPV6_MSG_REPLY: - case DHCPV6_MSG_RECONFIGURE: - case DHCPV6_MSG_RELAY_REPL: - case DHCPV6_MSG_DHCPV4_RESPONSE: -#ifndef DHCPV4_SUPPORT - case DHCPV6_MSG_DHCPV4_QUERY: -#endif - default: - return; /* Invalid message types for clients */ - } - if (!IN6_IS_ADDR_MULTICAST((struct in6_addr *)dest_addr) && iov[IOV_NESTED].iov_len == 0 && (hdr->msg_type == DHCPV6_MSG_SOLICIT || hdr->msg_type == DHCPV6_MSG_CONFIRM || hdr->msg_type == DHCPV6_MSG_REBIND || hdr->msg_type == DHCPV6_MSG_INFORMATION_REQUEST)) diff --git a/src/dhcpv6.h b/src/dhcpv6.h index a394453..4220fe2 100644 --- a/src/dhcpv6.h +++ b/src/dhcpv6.h @@ -22,6 +22,7 @@ #define DHCPV6_CLIENT_PORT 546 #define DHCPV6_SERVER_PORT 547 +/* RFC8415 */ #define DHCPV6_MSG_SOLICIT 1 #define DHCPV6_MSG_ADVERTISE 2 #define DHCPV6_MSG_REQUEST 3 @@ -35,6 +36,7 @@ #define DHCPV6_MSG_INFORMATION_REQUEST 11 #define DHCPV6_MSG_RELAY_FORW 12 #define DHCPV6_MSG_RELAY_REPL 13 +/* RFC7341 */ #define DHCPV6_MSG_DHCPV4_QUERY 20 #define DHCPV6_MSG_DHCPV4_RESPONSE 21 -- 2.30.2